package com.example.dayday.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dayday.common.R;
import com.example.dayday.config.StudentListeren;
import com.example.dayday.pojo.*;
import com.example.dayday.service.*;
import com.example.dayday.vo.StudentVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Helen
 * @since 2022-09-07
 */
@RestController
@RequestMapping("/studentCourse")
public class StudentCourseController {
    @Autowired
    private StudentCourseService studentCourseService;

    @Autowired
    private SpecializedService specializedService;
    @Autowired
    private StudentService studentService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "选课")
    @GetMapping("/xuanke")
    public R xuanke(HttpSession session, Long courseId) {
        Student user = (Student) session.getAttribute("user");
        StudentCourse studentCourse = new StudentCourse();

        Long studentId = user.getStudentId();
        LambdaQueryWrapper<StudentCourse> wq = new LambdaQueryWrapper<>();
        wq.eq(StudentCourse::getStudentId, studentId);
        List<StudentCourse> list = studentCourseService.list(wq);

        if (list.size() < 3) {
            LambdaQueryWrapper<Course> we = new LambdaQueryWrapper<>();
            we.eq(Course::getCourseId, courseId);
            Course one = courseService.getOne(we);
            if (one.getMaxNumber() != 0) {
                one.setMaxNumber(one.getMaxNumber() - 1);
                one.setNumber(one.getNumber() + 1);
                courseService.updateById(one);
            } else {
                return R.ok().message("失败");
            }
            studentCourse.setStudentId(studentId);
            studentCourse.setCourseId(courseId);
            studentCourseService.save(studentCourse);

            Message message = new Message();
            message.setMsg("1");
            message.setStudentId(studentId);
            message.setCourseId(courseId);
            message.setCreateTime(LocalDateTime.now());
            messageService.save(message);
            return R.ok().message("成功");
        }
        return R.error().message("失败，你已选择了三门课");
    }

    @GetMapping("/exportData")
    public R exportData() {
        String fileName = "d:\\01.xlsx";
        List<Student> list = studentService.list();
        String s = JSON.toJSONString(list);
        List<StudentVo> studentVos = JSON.parseArray(s, StudentVo.class);

        List<StudentCourse> StudentCourseList = studentCourseService.list();
        List<Course> Courselist = courseService.list();
        for (StudentVo studentVo : studentVos) {
            String cname = "";
            for (StudentCourse ss : StudentCourseList) {
                for (int i = 0; i < Courselist.size(); i++) {
                    if (ss.getCourseId().equals(Courselist.get(i).getCourseId())) {
                        if (ss.getStudentId().equals(studentVo.getStudentId())) {
                            cname += Courselist.get(i).getCourseName() + ",";
                        }
                    }
                    studentVo.setCourseName(cname);
                }
            }
        }
        List<Specialized> specializedList = specializedService.list();
        for (StudentVo studentVo : studentVos) {
            for (int i = 0; i < specializedList.size(); i++) {
                if (studentVo.getClassId().equals(specializedList.get(i).getClassId())) {
                    studentVo.setSpecialized(specializedList.get(i).getSpecialized());
                }

            }
            if (studentVo.getSex().equals("1")) {
                studentVo.setSex("男");
            } else {
                studentVo.setSex("女");
            }
        }
        EasyExcel.write(fileName, StudentVo.class).sheet("用户信息").doWrite(studentVos);
        return R.ok().message("成功");
    }
}
